﻿Imports System.Data
Imports System.Data.SqlClient
Imports FreeTextBoxControls
Partial Class nctnhomsp
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "
    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Dim blnAddNew As Boolean
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim intID As Int16
        If Not Page.IsPostBack Then
            hideText.Text = -1
            blnAddNew = True
            Button1.Text = "Thêm mới"
            If IsNumeric(Request.QueryString("g")) Then
                intID = Request.QueryString("g")
                BindGrid(intID)
                blnAddNew = False
                Button1.Text = "Cập nhật"
            End If
            Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
            Dim sSql As String = "select idnn,ngonngu from ngonngu"
            myConn.Open()
            Dim myAdapter As New SqlDataAdapter(sSql, myConn)
            Dim myDataSet2 As New DataSet
            myAdapter.Fill(myDataSet2, sSql)
            idnn.DataSource = myDataSet2
            idnn.DataBind()
            sSql = "select tenloaisp,idloaisp from loaisp"
            Dim myAdapter1 As New SqlDataAdapter(sSql, myConn)
            Dim myDataSet3 As New DataSet
            myAdapter1.Fill(myDataSet3, sSql)
            cboLoaisp.DataSource = myDataSet3
            cboLoaisp.DataBind()
            myConn.Close()
            myConn.Close()

        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sSql As String
        If hideText.Text <= 0 Then
            sSql = "insert into nhomsp (tennhomsp,idnn,gioithieu,hinhnho,tieude,TT,thutu,idvn, tieude_nhomsp, gioithieu_nhomsp, tukhoa_nhomsp) " & _
                    "values(@tennhomsp,@idnn,@gioithieu,@hinhnho,@tieude,@TT,@thutu,@idvn, @tieude_nhomsp, @gioithieu_nhomsp, @tukhoa_nhomsp)"
            blnAddNew = True
        Else
            sSql = "update nhomsp set" & _
            " tennhomsp=@tennhomsp,idnn=@idnn,gioithieu=@gioithieu" & _
            " ,hinhnho=@hinhnho,tieude=@tieude,TT=@TT,thutu=@thutu,idvn=@idvn" & _
            " ,tieude_nhomsp=@tieude_nhomsp,gioithieu_nhomsp=@gioithieu_nhomsp,tukhoa_nhomsp=@tukhoa_nhomsp " & _
            " where idnhomsp=@idnhomsp"
            blnAddNew = False
        End If
        Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
        Dim MyCommand As SqlCommand
        myConn.Open()
        MyCommand = New SqlCommand(sSql, myConn)
        If Not blnAddNew Then
            MyCommand.Parameters.Add(New SqlParameter("@idnhomsp", hideText.Text))
        End If
        MyCommand.Parameters.Add(New SqlParameter("@tennhomsp", tennhomsp.Text))
        MyCommand.Parameters.Add(New SqlParameter("@TT", TT.Checked))
        MyCommand.Parameters.Add(New SqlParameter("@idnn", idnn.SelectedValue))
        MyCommand.Parameters.Add(New SqlParameter("@thutu", CInt(Thutu.Text)))
        MyCommand.Parameters.Add(New SqlParameter("@hinhnho", hinhnho.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tieude", tieude.Text))
        MyCommand.Parameters.Add(New SqlParameter("@gioithieu", gioithieu.Text))
        MyCommand.Parameters.Add(New SqlParameter("@idvn", IIf(idvn.Text = "", 0, idvn.Text)))
        MyCommand.Parameters.Add(New SqlParameter("@tieude_nhomsp", tieudetrang.Text))
        MyCommand.Parameters.Add(New SqlParameter("@gioithieu_nhomsp", mota.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tukhoa_nhomsp", tukhoa.Text))
        MyCommand.ExecuteNonQuery()
        If blnAddNew Then
            sSql = "select max(idnhomsp) from nhomsp"
            MyCommand.CommandText = sSql
            hideText.Text = MyCommand.ExecuteScalar
        End If
        sSql = "update nhomsp set noidung =N'" & Replace(noidung.Text, "'", "''") & "' where idnhomsp =" & hideText.Text
        MyCommand.CommandText = sSql
        MyCommand.ExecuteNonQuery()
        myConn.Close()
        Response.Redirect("nnhomsp.aspx")
    End Sub

    Private Sub BindGrid(ByVal vId As Int16)
        On Error Resume Next
        Dim dr As DataRow
        Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
        Dim sSql As String = "select top 1 * from nhomsp where idnhomsp = " & vId & " order by thutu"
        myConn.Open()
        Dim myComm As New SqlCommand(sSql, myConn)
        Dim myDr As SqlDataReader
        myDr = myComm.ExecuteReader
        If myDr.Read Then
            tennhomsp.Text = myDr("tennhomsp")
            TT.Checked = myDr("TT")
            Thutu.Text = myDr("thutu")
            gioithieu.Text = myDr("gioithieu")
            noidung.Text = myDr("noidung")
            idnn.SelectedValue = myDr("idnn")
            cboLoaisp.SelectedValue = myDr("idloaisp")
            hinhnho.Text = myDr("hinhnho")
            tieude.Text = myDr("tieude")
            idvn.Text = myDr("idvn")
            hideText.Text = myDr("idnhomsp")
            tieudetrang.Text = myDr("tieude_nhomsp")
            mota.Text = myDr("gioithieu_nhomsp")
            tukhoa.Text = myDr("tukhoa_nhomsp")
        End If
        myConn.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If idnn.SelectedValue = 1 Then idvn.Text = hideText.Text
        hideText.Text = -1
        Button1.Text = "Thêm mới"
    End Sub

End Class

